/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.view.controller;

import cz.cvut.fel.taskpro.data.entity.TemplateFile;
import cz.cvut.fel.taskpro.data.entity.VersionFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.activation.MimetypesFileTypeMap;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

/**
 * Abstract controller which implements downloading and uploading different types
 * of files.
 * @author Tom
 * @version 1.0.0. beta
 */
public abstract class AbstractFileController extends AbstractController {

    private List<UploadedFile> uploadedFile;

    public List<UploadedFile> getUploadedFile() {
        return uploadedFile;
    }

    public void setUploadedFile(List<UploadedFile> uploadedFile) {
        this.uploadedFile = uploadedFile;
    }
    /**
     * This method convert input stream to file.
     * @param fileName name of output file
     * @param input input stream to convert to file
     * @return file which will be converted from input stream in parameter
     */
    protected File inputStreamToFile(InputStream input, String fileName) {
        File f = new File(fileName);
        byte[] bytes = new byte[1024];
        try {
            FileOutputStream out = new FileOutputStream(f);
            int len;
            try {
                while ((len = input.read(bytes)) > 0) {
                    out.write(bytes, 0, len);
                }
                out.close();
                input.close();
            } catch (IOException ex) {
            }
        } catch (FileNotFoundException ex) {
        }
        return f;
    }
    /** 
     * This method create streamed contend from bytes. Name of file is specifi in
     * entity param of parameter. Streamed content must be used to download via 
     * PrimeFaces.
     * @param versionFile entity from which will be created stream content.
     * @return streamedContent from parameter. StreamedContent is used to download via PrimeFaces.
     */
    public StreamedContent downloadFilesByTask(VersionFile versionFile) {
        byte[] fileByte = versionFile.getFile();
        MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        String fileName = versionFile.getFileName();
        String mimeType = mimeTypesMap.getContentType(fileName);
        InputStream fStream = new ByteArrayInputStream(fileByte);
        StreamedContent filess = new DefaultStreamedContent(fStream, mimeType, fileName);
        return filess;
    }
       /** 
     * This method create streamed contend from bytes. Name of file is specifi in
     * entity param of parameter. Streamed content must be used to download via 
     * PrimeFaces.
     * @param templateFile  entity from which will be created stream content.
     * @return streamedContent from parameter. StreamedContent is used to download via PrimeFaces.
     */
    public StreamedContent download(TemplateFile templateFile) {
        byte[] fileByte = templateFile.getFile();
        MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        String fileName = templateFile.getFileName();
        String mimeType = mimeTypesMap.getContentType(fileName);
        InputStream fStream = new ByteArrayInputStream(fileByte);
        StreamedContent filess = new DefaultStreamedContent(fStream, mimeType, fileName);
        return filess;
    }
}
